﻿using System.Xml;
using System.IO;
using System;
using System.Collections.Generic;
using System.Text;

namespace xReplace
{
    class DebugObject
    {
        public DebugObject(Project p)
        {
            this.enabled = p.GetKeyAsBoolean("debug", "enabled");
            this.displayScript = p.GetKeyAsBoolean("debug", "displayScript");
            this.displayContent = p.GetKeyAsBoolean("debug", "displayContent");
            this.logFile = p.GetKey("debug", "logFile");
            this.dumpToConsole = p.GetKeyAsBoolean("debug", "outputToConsole");

            if (File.Exists(this.logFile)) File.Delete(this.logFile);
        }

        public bool enabled;
        public bool displayContent;
        public bool displayScript;
        public bool dumpToConsole;
        public string logFile;

        public void Log(string msg)
        {
            if(this.enabled) File.AppendAllText(this.logFile, msg + "\n");
            if ((this.enabled) && (this.dumpToConsole)) Console.WriteLine(msg);
        }

        public void Log(string msg, int ptr, XmlNodeList nl, Content pipelines)
        {
            if (this.enabled)
            {
                if (this.displayScript)
                {
                    this.Log("Processing pointer:" + ptr + ", command: " + nl[ptr].Name.ToLower());
                    this.Log("Command parameters:\n" + nl[ptr].InnerXml);
                }

                if (this.displayContent)
                {
                    Dictionary<string,string>.Enumerator pe =  pipelines.GetEnumerator();
                    this.Log("Content Pipelines:");
                    this.Log("- - - - - - - - - - - - - -");
                    this.Log(" ");
                    while (pe.MoveNext())
                    {
                        this.Log("Label: " + pe.Current.Key);
                        this.Log(pe.Current.Value);
                        this.Log("- - - - - - - - - - - - - -");
                        this.Log(" ");
                    }
                }
            }
        }
    }
}
